home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10342 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  103 lines

  1. Path: aixterm4.urz.uni-heidelberg.de!abangert
  2. From: abangert@aixterm4.urz.uni-heidelberg.de (Achim Bangert)
  3. Newsgroups: comp.lang.c
  4. Subject: Error in fp-exception handling?
  5. Date: 17 Mar 1996 02:06:09 GMT
  6. Organization: University of Heidelberg, Germany
  7. Message-ID: <4ifs2i$ld2@sun0.urz.uni-heidelberg.de>
  8. NNTP-Posting-Host: aixterm4.urz.uni-heidelberg.de
  9. Keywords: floating point exception
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Hello,
  13.  
  14. I have a problem concerning the detection of floating point exceptions. 
  15. Running the following program:
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <errno.h>
  23. #include <signal.h>
  24.  
  25. void sighandler(int signo)
  26. {
  27.   fprintf(stderr, "Catched signal no. %d: %s\n", signo, strsignal(signo));
  28.   fprintf(stderr, "Error no. %d: %s\n", errno, strerror(errno));
  29.   exit(1);
  30. }
  31.  
  32. int main(void)
  33. {
  34.   double a, b, c, d;
  35.   double x, y, z;
  36.   struct sigaction action;
  37.  
  38.   action.sa_handler = sighandler;
  39.   sigemptyset(&action.sa_mask);
  40.   action.sa_flags = 0;
  41.   if(sigaction(SIGFPE, &action, NULL) < 0)
  42.     exit(1);
  43.   printf("Please input four double values:\n");
  44.   scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
  45.   errno = 0;
  46.   x = a * b;
  47.   printf("%16.11e * %16.11e = %16.11e  error no. %d\n", a, b, x, errno);
  48.   errno = 0;
  49.   y = log(c);
  50.   printf("ln(%16.11e) = %16.11e  error no. %d\n", c, y, errno);
  51.   errno = 0;
  52.   z = exp(d);
  53.   printf("exp(%16.11e) = %16.11e  error no. %d\n", d, z, errno);
  54.   exit(0);
  55. }
  56.  
  57.  
  58. I get some confusing results. For expample:
  59.  
  60.  
  61. Please input four double values:
  62. 1e100 2e250 3 4
  63. 1.00000000000e+100 * 2.00000000000e+250 = 3.59363441623e+77  error no. 0
  64. Catched signal no. 8: Floating point exception
  65. Error no. 2: No such file or directory
  66.  
  67. (no detection of the overflow)
  68.  
  69. Please input four double values:
  70. 1 2 3 1234
  71. 1.00000000000e+00 * 2.00000000000e+00 = 2.00000000000e+00  error no. 0
  72. ln(3.00000000000e+00) = 1.09861228867e+00  error no. 0
  73. exp(1.23400000000e+03) = -1.99808192253e+00  error no. 0
  74.  
  75. (the same here, but not in the following example)
  76.  
  77. Please input four double values:
  78. 1 2 3 12345
  79. 1.00000000000e+00 * 2.00000000000e+00 = 2.00000000000e+00  error no. 0
  80. ln(3.00000000000e+00) = 1.09861228867e+00  error no. 0
  81. Catched signal no. 8: Floating point exception
  82. Error no. 2: No such file or directory
  83.  
  84.  
  85. My computer has a pentium processor and is running under Linux 1.2.13.
  86. I have compiled this program with "gcc -o fptest fptest.c -lm" using
  87. gcc 2.7.0, libm 4.6.27 
  88. Can anybody help me with this problem? Did I make something wrong or is the
  89. system to blame (hardware or software)? I asked some friends of mine to
  90. run this program and depending on processor type and kernel/compiler version
  91. they could reproduce this behaviour or not. Furthermore, in the info files to
  92. glibc (0.06 DRAFT) is mentioned that the library functions would not raise
  93. SIGFPE but set errno instead. Is this no longer valid?
  94.  
  95. Hopefully somebody knows the answer to my question.
  96.  
  97. Thanks,
  98. Achim Bangert 
  99.  
  100. --
  101. --------------------------------------------------------------
  102. Achim Bangert                abangert@ix.urz.uni-heidelberg.de
  103.